package img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.text.AttributedString;
import java.util.Random;

/**
 * @author wzby
 * @Date: 2022/8/3 19:30
 */
public class Ex1 {
    public static void main(String[] args) {
        File f = new File("ch09/src/pho/ai1.jpg");
        try {
            var src = ImageIO.read(f);
            var w = src.getWidth();
            var h = src.getHeight();
            var g = src.createGraphics();

            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

            g.setColor(Color.ORANGE);
            g.drawLine(0, h/2,w,h/2);
            g.setColor(Color.WHITE);
            g.drawLine(w/2,0,w/2,h);

            //颜色
            var c = new Color(0,255,0,120);
            g.setColor(c);

            //字体
            Font z = Font.createFont(Font.TRUETYPE_FONT,new File("E:\\uexdir\\font\\wzby.ttf"));
            z = z.deriveFont(Font.BOLD,65);
            g.setFont(z);

            //g.drawString("张北王珂",200,200);

           int x= 10;
           int y= 55;
            String txt = "0420张北王珂";
            FontMetrics fm = g.getFontMetrics(z);
            int sw = fm.stringWidth(txt);
            int sa = fm.getAscent();


            //左上角
            x = 0;
            y = sa + 10;
            //正中央
            //x = (2-sw)/2;
            //y = (h+sa)/2;
            //右下角
            //x = w - sw - 10;
            //y = h - 10;

            //随机出现
            //Random rand = new Random();
            //x = rand.nextInt(10,w-10-sw);
           // y = rand.nextInt(10+sa,h-10);
            g.drawString(txt,x,y);
            g.dispose();

            f = new File("ch09/src/pho/ai2.jpg");
            ImageIO.write(src,"jpg",new File("ch09/src/pho/ai2.jpg"));
            Runtime.getRuntime().exec("cmd /k start ch09/src/pho/ai2.jpg");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
